package com.dyava.commons.lock.complex;

import com.dyava.commons.lock.KeyLock;
import com.dyava.commons.lock.KeyLockException;
import com.dyava.commons.lock.KeyLockPool;

import java.util.ArrayList;
import java.util.List;

public class ComplexKeyLockPool extends KeyLockPool {
    List<KeyLockPool> lockPoolList;

    public List<KeyLockPool> getLockPoolList() {
        return lockPoolList;
    }

    public void setLockPoolList(List<KeyLockPool> lockPoolList) {
        this.lockPoolList = lockPoolList;
    }

    public ComplexKeyLockPool() {
    }

    public ComplexKeyLockPool(List<KeyLockPool> lockPoolList) {
        this.lockPoolList = lockPoolList;
    }

    public KeyLock createLock(String key){
        if(lockPoolList==null || lockPoolList.isEmpty()){
            throw new KeyLockException("lockPoolList 不能为空");
        }
        List<KeyLock> keyLockList = new ArrayList<>(lockPoolList.size());
        for(KeyLockPool keyLockPool : lockPoolList){
            keyLockList.add(keyLockPool.createLock(key));
        }
        return new ComplexKeyLock(keyLockList);
    };

}
